package com.example.activity;

import java.lang.reflect.Method;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.R;
import com.example.module.widget.CustomWebView;

public class TestActivity extends Activity {
	private static final int LOW_DPI_STATUS_BAR_HEIGHT = 19;
	private static final int MEDIUM_DPI_STATUS_BAR_HEIGHT = 25;
	private static final int HIGH_DPI_STATUS_BAR_HEIGHT = 38;

	private CustomWebView webView;
	private View titleBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test);
		titleBar = LayoutInflater.from(this).inflate(R.layout.address_bar,  null);

		webView = (CustomWebView) findViewById(R.id.webView);
		webView.setWebViewClient(new WebViewClient() {
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
			}
		});

//		webView.setEmbeddedTitleBar(titleBar);
//		webView.setEmbeddedTitleBarCompat(titleBar);
//		int statusBarHeight = getStatusBarHeight();
//		webView.setStatusBarHeight(statusBarHeight);

		try {
			Method method = WebView.class.getMethod("setEmbeddedTitleBar",
					new Class[] { View.class });
			method.invoke(webView, titleBar);
		} catch (Exception ex) {
//			webView.setAddressBar(titleBar);
//			
//			int statusBarHeight = getStatusBarHeight();
//			webView.setStatusBarHeight(statusBarHeight);
			
			ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
					ViewGroup.LayoutParams.MATCH_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
			webView.addView(titleBar, 0, params);

			ex.printStackTrace();
		}

		webView.loadUrl("http://m.naver.com");
	}
	
	public int getStatusBarHeight() {
		DisplayMetrics displayMetrics = new DisplayMetrics();
		((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(displayMetrics);

		int statusBarHeight = 0;

		switch (displayMetrics.densityDpi) {
		    case DisplayMetrics.DENSITY_HIGH:
		        statusBarHeight = HIGH_DPI_STATUS_BAR_HEIGHT;
		        break;
		    case DisplayMetrics.DENSITY_MEDIUM:
		        statusBarHeight = MEDIUM_DPI_STATUS_BAR_HEIGHT;
		        break;
		    case DisplayMetrics.DENSITY_LOW:
		        statusBarHeight = LOW_DPI_STATUS_BAR_HEIGHT;
		        break;
		    default:
		        statusBarHeight = MEDIUM_DPI_STATUS_BAR_HEIGHT;
		}
		
		return statusBarHeight;
	}
}
